import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import axios from 'axios'


import useUserStore from "@/store/user";
import { inject ,ref,} from "vue";
import pinia from '@/store'
useUserStore(pinia)

const routes = [
  {
    path: "/",
    name: 'login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: "/index",
    name: 'index',
    component: () => import('@/views/index/index.vue'),
    children:[
      // {
      //   path: "/home",
      //   name: '首页',
      //   meta:{
      //     title:'首页'
      //   },
      //   component: () => import('@/views/home/index.vue'),
      // },
    ],
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


const modules = import.meta.glob("../views/**")
console.log('modules',modules);

 function getRouteAddList() {
    const _store = inject('store')
  let  params = JSON.parse(sessionStorage.getItem('NavList')) 
  let list =[]
    params.map(item=>{
      if(typeof(item.web_path) == 'string'){
        console.log('item.web_path',item.web_path,'modules[`../views${item.web_path}/index.vue`',modules[`../views${item.web_path}/index.vue`]);
        let routerChildrenOne = {
          path:item.web_path,
            name:item.web_path.slice(1),
            component:modules[`../views${item.web_path}/index.vue`],
            meta:{
              title:item.name,
              buts:[],
              requireAuth: true,
              keepAlive: true
              }
        }
        list.push(routerChildrenOne)
        let butOne:any = item.slug
        routerChildrenOne.meta.buts.push(butOne)

      }else{
        item.children.map(Citem=>{
          console.log('Citem.web_path',Citem.web_path,'modules[`../views${Citem.web_path}/index.vue`',modules[`../views${Citem.web_path}/index.vue`]);
          let routerChildren = {
            path:Citem.web_path,
            name:Citem.web_path.slice(1),
            component:modules[`../views${Citem.web_path}/index.vue`],
            meta:{
              title:Citem.name,
              buts:[],
              requireAuth: true,
              keepAlive: true
              }
          }
          list.push(routerChildren)
          Citem.children.map(C_item_C=>{
            let but = C_item_C.slug
            routerChildren.meta.buts.push(but)
          })
        })
      }
    })
    const parentRoute = router.getRoutes().find(route => route.name === 'index');
    list.forEach(item =>{
      parentRoute.children.push(item)
    })
    console.log('routers',router);
    
    router.addRoute(parentRoute)
}

let isOneLogin = true
router.beforeEach((to, from, next) => {
  // //是否第一次进入
  if(isOneLogin){
    if(sessionStorage.getItem('NavList')){
      getRouteAddList()
    }
    isOneLogin = false
  }
  next();
    const _store = inject('store')
  // if (to.fullPath != '/' && to.fullPath != '/index') {
    _store.getNowRouterPath(to)
    
  // }
  // const isAuthenticated = checkIfUserIsAuthenticated();

  // if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
  //   // 如果用户试图访问需要身份验证的页面，但未经过身份验证，则重定向到登录页面
  //   next({ path: '/' });
  // } 


  // // else if (to.name === 'login' && isAuthenticated) {
  // //   // 如果用户已经通过身份验证，并且访问根路径，则直接重定向到主页
  // //   next({ path: 'home' });
  // // }
  
  
  
  // else {
  //   // 如果用户已经通过身份验证，并且访问的页面不需要身份验证，则继续导航
  //     next();
  // }
  

});
function checkIfUserIsAuthenticated() {
  // 检查本地存储中的认证状态
  const isAuthenticated = localStorage.getItem('isAuthenticated');
  // 如果认证状态为true，则返回true；否则返回false
  return isAuthenticated === 'true';
}
export default router